/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.account.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.account.domain.AccountAc08;
import me.zhengjie.modules.account.domain.dto.AccountAc08QueryCriteria;
import me.zhengjie.modules.account.factory.service.AccountComputer;
import me.zhengjie.modules.account.service.AccountAc08Service;
import me.zhengjie.modules.account.service.AccountAc50Service;
import me.zhengjie.modules.account.service.AccountService;
import me.zhengjie.modules.basic.domain.BasicAc02;
import me.zhengjie.modules.basic.mapper.BasicAc02Mapper;
import me.zhengjie.modules.logs.domain.BusinessLog;
import me.zhengjie.modules.logs.service.BusinessLogService;
import me.zhengjie.modules.sequence.domain.Sequence;
import me.zhengjie.modules.sequence.service.SequenceService;
import me.zhengjie.tool.MyMap;
import me.zhengjie.tool.ValidateUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.vo.Constant;
import me.zhengjie.vo.YHConstants;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author lzw
* @date 2025-06-09
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "api")
@RequestMapping("/api/accountAc50")
public class AccountAc50Controller {

    private final AccountService accountService;
    private final AccountAc50Service accountAc50Service;
    @Resource
    private final AccountComputer accountComputer;
    @Resource
    private final BasicAc02Mapper basicAc02Mapper;

    @Resource
    private final SequenceService sequenceService;

    @PostMapping("computerAccount")
    public ResponseEntity<Object> computerAccount(@RequestBody MyMap resources) throws IOException {
        accountService.saveInterest(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PostMapping("printAccount")
    public ResponseEntity<Object> printAccount(@RequestBody MyMap resources){
            return new ResponseEntity<>(accountService.print(resources),HttpStatus.OK);

    }

    @PostMapping("printAccountTitle")
    public ResponseEntity<Object> printAccountTitle(@RequestBody MyMap resources){

            HashMap map = new HashMap<>();
            map.put("data",accountService.print(resources).get("baseInfo"));
            return new ResponseEntity<>(map,HttpStatus.OK);

    }

    @PostMapping("printAccountBody")
    public ResponseEntity<Object> printAccountBody(@RequestBody MyMap resources){
            HashMap map = new HashMap<>();
            map.put("data",accountService.print(resources).get("accountInfo"));
            return new ResponseEntity<>(map,HttpStatus.OK);

    }

    @PostMapping("/getAc50")
    @ApiOperation("查询人员账户信息")
    public ResponseEntity<Map> getPersonFrees(@RequestBody Map resources){
        return new ResponseEntity<>(accountAc50Service.getAc50List(resources),HttpStatus.OK);
    }

    @PostMapping("/getAc50All")
    @ApiOperation("查询人员所有账户信息")
    public ResponseEntity<Map> getAc50All(@RequestBody Map resources){
        return new ResponseEntity<>(accountAc50Service.getAc50AllList(resources),HttpStatus.OK);
    }

    @PostMapping("/save")
    @ApiOperation("单位人员退保")
    public ResponseEntity<Object> save(@RequestBody Map resources) throws Exception {
        ValidateUtil.isEmptyThrowException(resources.get("aac001"),"个人编号");
        ValidateUtil.isEmptyThrowException(resources.get("accountList"),"账户列表");
        resources.put("aaz002",sequenceService.nextSequence(new Sequence("aaz002")));
        resources.put("aaa121", YHConstants.AAA121_TF);
        resources.put("aae016",Constant.AAE016_1_SHTG);
        resources.put("aae140",Constant.AAE140_120_JGSYDWGZRYJBYLBX);


        MyMap param = new MyMap(resources);
        // 计息
        MyMap myMap = new MyMap();
        List<BasicAc02> ac02List = new ArrayList<>();
        BasicAc02 basicAc02 = new BasicAc02();
        basicAc02.setAac001(param.getAsString("aac001"));
        basicAc02.setAac008(Constant.AAC008_1_ZCCB);
        basicAc02.setAae140(Constant.AAE140_120_JGSYDWGZRYJBYLBX);
        List<BasicAc02> ac02s = basicAc02Mapper.getAc02(basicAc02);
        if (!ValidateUtil.isEmpty(ac02s)) {
            ac02List = ac02s.stream().filter(item ->!Constant.AAC031_3_ZZJF.equals(item.getAac031())).collect(Collectors.toList());
        }
        if (ValidateUtil.isEmpty(ac02List)) {
            throw new RuntimeException("没有获取到正常参保的参保关系");
        }
        for (BasicAc02 basicac02:ac02List) {
            myMap.put("aac001",param.getAsString("aac001"));
            myMap.put("aaz159",basicac02.getAaz159());
            accountComputer.computerAccountNotSave(myMap);
        }
        // 重新取账户
        myMap.remove("aaz159");
        myMap.put("aae100",Constant.AAE100_1_YX);
        myMap.put("aae140",Constant.AAE140_120_JGSYDWGZRYJBYLBX);
        Map ac50Map = accountAc50Service.getAc50List(myMap);
        List<Map> accountList = (List<Map>) ac50Map.get("ac50");
        resources.put("oldAccountList",accountList);
        accountAc50Service.saveAc50(new MyMap(resources));
        return new ResponseEntity<>(HttpStatus.CREATED);
    }



}
